#include <pthread.h>
#include <stdio.h>

static int arr[5] = {0};
static int len = 0;

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void *f(void *pv) {
	static int i = 0;
	while (1) {
		pthread_mutex_lock(&mutex);
		while (len == 5) {
			pthread_cond_wait(&cond, &mutex);
		}
		arr[len] = i;
		++i;
		++len;
		printf("set\n");
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&mutex);
	}
	return NULL;
}
void *f2(void *pv) {
	while (1) {
		pthread_mutex_lock(&mutex);
		while (len == 0) {
			pthread_cond_wait(&cond, &mutex);
		}
		arr[len] = 0;
		--len;
		printf("get\n");
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&mutex);
	}
	return NULL;
}

int main(void) {
	pthread_t p;
	pthread_t p2;
	pthread_create(&p, NULL, f, NULL);
	pthread_create(&p, NULL, f2, NULL);
	pthread_join(p, NULL);
	pthread_join(p2, NULL);
	return 0;
}